package com.plants.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plants.back.pojo.PlantsGoods;
import com.plants.back.pojo.Vo;
import com.plants.back.service.impl.PlantsGoodsServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 商品控制器
 * </p>
 *
 * @author plants
 * @since 2024-08-06
 */
@CrossOrigin
@RestController
@RequestMapping("/plants_goods")
public class PlantsGoodsController {

    @Resource
    private PlantsGoodsServiceImpl goodsService;

    // 新增商品
    @PostMapping("/insert")
    public Vo<PlantsGoods> insert(@RequestBody PlantsGoods goods) {
        // 设置成为本地时间
        goods.setCreateTime(LocalDateTime.now());
        if (goods.getDistribution() == "" || goods.getDistribution() == null) {
            goods.setDistribution("全国可配送");
        }
        boolean b = goodsService.save(goods);
        if (b) {
            // 新增成功
            return new Vo<>(b, "添加成功！");
        } else {
            // 新增失败
            return new Vo<>(b, "添加失败，请稍后重试！");
        }
    }

    // 修改商品信息
    @PostMapping("/update")
    public Vo<PlantsGoods> update(@RequestBody PlantsGoods goods) {
        boolean b = goodsService.updateById(goods);
        if (b) {
            // 修改成功
            return new Vo<>(b, "修改商品信息成功！");
        } else {
            // 修改失败
            return new Vo<>(b, "修改商品信息失败，请稍后重试！");
        }
    }

    // 主页查询商品5条
    @GetMapping("/home")
    public Vo<PlantsGoods> home() {
        Page<PlantsGoods> goodsPage = new Page<>(1, 5);
        IPage<PlantsGoods> page = goodsService.page(goodsPage);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "当前并没有商品！");
        }
        return new Vo<>(true, page.getRecords());
    }

    // 分页查询商品
    @GetMapping("/page/{current}")
    public Vo<PlantsGoods> page(@PathVariable("current") Integer current) {
        Page<PlantsGoods> goodsPage = new Page<>(current <= 0 ? 1 : current, 6);
        IPage<PlantsGoods> page = goodsService.page(goodsPage);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "当前并没有商品！");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }

    // 根据商品名称查询商品
    @GetMapping("/search/{name}/{current}")
    public Vo<PlantsGoods> search(@PathVariable("name") String name, @PathVariable("current") Integer current) {
        Page<PlantsGoods> goodsPage = new Page<>(current <= 0 ? 1 : current, 6);
        LambdaQueryWrapper<PlantsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(PlantsGoods::getName, name);
        Page<PlantsGoods> page = goodsService.page(goodsPage, wrapper);
        if (page.getTotal() == 0) {
            // 表示没有数据
            return new Vo<>(true, "目前没有数据");
        } else {
            return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
        }
    }

    // 更加商品id查询单个商品
    @GetMapping("/queryOne/{id}")
    public Vo<PlantsGoods> queryOne(@PathVariable("id") String id) {
        LambdaQueryWrapper<PlantsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlantsGoods::getId, id);
        PlantsGoods goods = goodsService.getOne(wrapper);
        return new Vo<>(true, goods);
    }

    // 根据商品的分类查询不同的商品
    @GetMapping("/type/{type}/{current}")
    public Vo<PlantsGoods> type(@PathVariable("type") String type, @PathVariable("current") Integer current) {
        Page<PlantsGoods> goodsPage = new Page<>(current <= 0 ? 1 : current, 6);
        LambdaQueryWrapper<PlantsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlantsGoods::getType, type);
        Page<PlantsGoods> page = goodsService.page(goodsPage, wrapper);
        if (page.getTotal() == 0) {
            // 表示没有数据
            return new Vo<>(true, "目前没有数据");
        } else {
            return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
        }
    }

    // 删除商品
    @GetMapping("/delete/{id}")
    public Vo<PlantsGoods> delete(@PathVariable("id") String id) {
        UpdateWrapper<PlantsGoods> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        boolean b = goodsService.remove(wrapper);
        if (b) {
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }

}
